//
//  LCPersonViewController.m
//  EnterpriseCloud
//
//  Created by 东亨 on 2017/7/4.
//  Copyright © 2017年 lichao. All rights reserved.
//

#import "LCPersonViewController.h"
#import "LCCompanyViewController.h"
#import "LCSignViewController.h"
#import "LCMessageViewController.h"
#import "LCVersionViewController.h"
#import "LCModifyPassViewController.h"
#import "LCLoginViewController.h"
#import "ToastUtils.h"
@interface LCPersonViewController ()<UIAlertViewDelegate>
@property (weak, nonatomic) IBOutlet UIView *view1;
@property (weak, nonatomic) IBOutlet UIView *view2;
@property (weak, nonatomic) IBOutlet UIView *view3;
@property (weak, nonatomic) IBOutlet UIView *view4;
@property (weak, nonatomic) IBOutlet UIView *view5;
@property (weak, nonatomic) IBOutlet UIView *view6;
@property (weak, nonatomic) IBOutlet UIImageView *iconImg;
@property (weak, nonatomic) IBOutlet UILabel *companyLbl;
@property (weak, nonatomic) IBOutlet UILabel *phoneLbl;
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;
@property (weak, nonatomic) IBOutlet UIButton *exitBtn;

@end
@implementation LCPersonViewController
- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (get_sp(@"app_user"))
    {
        self.exitBtn.hidden = NO;
        self.companyLbl.text = get_sp(@"company_name");
        NSDictionary  *companyDic = [NSDictionary dictionaryWithDictionary:get_sp(@"company")];
        NSString *imgUrl = [NSString stringWithFormat:@"%@/%@%@",ENCLOUD_URL,companyDic[@"logo_path"],companyDic[@"logo_name"]];
        [self.iconImg sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:img(@"companyicon.png")];
    }
    else
    {
        self.iconImg.image = img(@"companyicon.png");
        self.exitBtn.hidden = YES;
        self.companyLbl.text = @"登录";
    }
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBarTitle:@"个人中心"];
    self.iconImg.layer.cornerRadius = CGRectGetWidth(self.iconImg.frame)/2.0;
    self.iconImg.layer.masksToBounds = YES;
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loadCartCount) name:nLogin object:nil];
    [self loadCustomeView];
}
- (void)loadCustomeView
{
    //点击手势
    UITapGestureRecognizer *recognizer;
    for (int i = 0; i < 6; i++)
    {
        recognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapGesture:)];
        switch (i) {
            case 0:
                [_view1 addGestureRecognizer:recognizer];
                break;
            case 1:
                [_view2 addGestureRecognizer:recognizer];
                break;
            case 2:
                [_view3 addGestureRecognizer:recognizer];
                break;
            case 3:
                [_view4 addGestureRecognizer:recognizer];
                break;
            case 4:
                [_view5 addGestureRecognizer:recognizer];
                break;
            case 5:
                [_view6 addGestureRecognizer:recognizer];
                break;
            default:
                break;
        }
    }
}
-(void)tapGesture:(UITapGestureRecognizer *)sender
{
    if (!get_sp(@"app_user"))
    {
        [ToastUtils show:@"请您登录后进行此项操作！"];
        //没有登录要先登录
        LCLoginViewController *vc = [[LCLoginViewController alloc]init];
        [self presentViewController:vc animated:YES completion:nil];
        return;
    }
    switch (sender.view.tag) {
        case 1:
        {
            //保修
            LCMessageViewController *vc = [[LCMessageViewController alloc]init];
            vc.typeStr = @"我的报修";
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 2:
        {
            //我的报名
            LCSignViewController *vc = [[LCSignViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 3:
        {
            //留言
            LCMessageViewController *vc = [[LCMessageViewController alloc]init];
             vc.typeStr = @"留言";
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 4:
        {
            //企业信息展示
            LCCompanyViewController *vc = [[LCCompanyViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 5:
        {
            //登录后直接修改密码
            LCModifyPassViewController *vc = [[LCModifyPassViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        case 6:
        {
            //版本说明
            LCVersionViewController *vc = [[LCVersionViewController alloc]init];
            [self.navigationController pushViewController:vc animated:YES];
            break;
        }
        default:
            break;
    }
}
- (IBAction)loginBtnClick:(UIButton *)sender
{
    if (get_sp(@"app_user"))
    {
        //企业信息展示
        LCCompanyViewController *vc = [[LCCompanyViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    else
    {
        //没有登录要先登录
        LCLoginViewController *vc = [[LCLoginViewController alloc]init];
        [self presentViewController:vc animated:YES completion:nil];
    }
}
- (IBAction)exitAppClick:(UIButton *)sender
{
    if (get_sp(@"app_user"))
    {
        SYSTEM_ALERT_SHOW(@"确定退出登录", self, 1);
        return;
    }
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 1)
    {
        remove_sp(@"app_user");
        remove_sp(@"app_pass");
        remove_sp(@"company_id");
        remove_sp(@"company_name");
        remove_sp(@"company");
        self.exitBtn.hidden = YES;
        self.companyLbl.text = @"登录";
        self.iconImg.image = img(@"companyicon.png");
    }
}
//登录成功
-(void)loadCartCount
{
    self.companyLbl.text = get_sp(@"company_name");
    self.exitBtn.hidden = NO;
    
    NSDictionary  *companyDic = [NSDictionary dictionaryWithDictionary:get_sp(@"company")];
    NSString *imgUrl = [NSString stringWithFormat:@"%@/%@%@",ENCLOUD_URL,companyDic[@"logo_path"],companyDic[@"logo_name"]];
    [self.iconImg sd_setImageWithURL:[NSURL URLWithString:imgUrl] placeholderImage:img(@"companyicon.png")];
}
@end
